package com.eriloan.easyPage.plugins.templates.velocity.directive;

import java.io.IOException;
import java.io.Writer;

import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.parser.node.Node;

import com.eriloan.easyPage.util.EasyPageContext;

/**
 * 
 * <p>Copyright: All Rights Reserved</p>
 * <p>Company: 北京荣之联科技股份有限公司 http://www.ronglian.com</p>
 * <p>Description: Velocity 自定义 Session 指令 </p>
 * <p>Author:Eric Shi/史丙利</p>
 */
public class SessionDirective extends EasyPageAbstractDirective{

	@Override
	public String getName(){
		return "Session";
	}

	@Override
	public int getType(){
		return SessionDirective.LINE;
	}

	@Override
	public boolean doRender(InternalContextAdapter internalContext,EasyPageContext context,Writer writer,Node node) throws IOException,ResourceNotFoundException,ParseErrorException,MethodInvocationException{

		Node sn_region = node.jjtGetChild(0);

		String sessionKey = sn_region.getFirstToken().toString().replace("\"","").trim();
		
		if(null != sessionKey && ! "".equalsIgnoreCase(sessionKey)){

			Object obj = context.getSession().getAttribute(sessionKey);

			if(null != obj && ! "".equalsIgnoreCase(obj.toString())){
				writer.write(obj.toString());
				return true;
			}else{
				writer.write("null");
				return false;
			}

		}else{
			writer.write("null");
			return false;
		}
	}
}
